<template>
	<!-- 商家二维码 -->
	<view class="huabao">
		<view class="container">
			<canvas canvas-id="qrcode" style="position: fixed;opacity: 0;"/>
			<canvas class="canvas" canvas-id="canvas" id="canvas" style="position: fixed;opacity: 0;"></canvas>
			<image :src="imgUrl" style="width: 640rpx;height: 1060rpx;"></image>
		</view>
		<view class="">
			<u-button :ripple="true" type="primary" style="width: 60%;" shape="circle" @click="saveImage">保存二维码
			</u-button>
		</view>
	</view>
</template>

<script>
	import uQRCode from '@/common/uQRCode.js';
	export default {
		data() {
			return {
				img: require('@/static/offline/my/bg.png'),
				img2: require('@/static/my/btn.png'),
				data: require('@/static/my/ewm.png'),
				imgUrl: '',
				merchantShortName: '',
				text: ''
			}
		},
		mounted() {
			this.$u.offlineApi.merchantInfo().then(res => {
				if (res.code == 0) {
					this.merchantShortName = res.data.merchantShortName;
					this.text = res.data.merchantQrCode;
					this.make();
				}
			})
		},
		methods: {
			make() {
				uQRCode.make({
					canvasId: 'qrcode',
					componentInstance: this,
					text: this.text,
					size: 150,
					margin: 0,
					backgroundColor: '#ffffff',
					foregroundColor: '#000000',
					fileType: 'jpg',
					correctLevel: uQRCode.defaults.correctLevel,
					success: res => {
						var canvas = uni.createCanvasContext('canvas', this);
						canvas.drawImage(this.img, 0, 0, 300, 530);
						canvas.drawImage(res, 79, 225, 142, 142);
						canvas.setTextAlign('center');
						canvas.setTextBaseline('middle')
						canvas.setFontSize(14);
						canvas.setFillStyle('#0079FB')
						canvas.fillText(this.merchantShortName, 150, 200);
						canvas.draw(false, () => {
							// 返回canvas图片信息
							uni.canvasToTempFilePath({
								canvasId: 'canvas',
								success: (res) => {
									this.imgUrl = res.tempFilePath
								}
							})
						})
					}
				})
				 
			},
			saveImage() {
				var _this = this;
				uni.saveImageToPhotosAlbum({
					filePath: _this.imgUrl,
					success() {
						uni.showModal({
							title: "保存成功",
							content: "图片已成功保存到相册，快去分享到您的圈子吧",
							showCancel: false
						})
					}
				})
			},
		}
	}
</script>

<style scoped>
	.huabao {
		padding-bottom: 60rpx;
	}

	.container {
		margin: 30rpx 0;
		display: flex;
		justify-content: center;
	}

	.canvas {
		width: 300px;
		height: 530px;
	}
</style>
